package com.outfood.service.impl;

import com.outfood.common.common.replay.ReplyCode;
import com.outfood.common.common.response.RestResponse;
import com.outfood.common.eleme.Header;
import com.outfood.common.eleme.Result;
import com.rys.common.utils.DateUtils;
import io.swagger.annotations.ApiOperation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.Year;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author huisonglin
 * @Description
 * @PackageName outfoodcat-master
 * @Package com.outfood.service.impl
 * @Date 2024-05-27 12:12
 * @Version 1.0By.xpath获取WebElement 子元素的class名称为cc的元素
 */
@Service
public class ElemeRemoteService {

    @Resource
    WebDriverMapService webDriverMapService;

//    public static void main(String[] args) {
//        String str = "#28\n13:09 前送达\n已呼叫配送商\n祁**\n近180天第1次下单\n联系不上\n1.68km ·三里亭东苑16幢52号301\n查看地图\n承诺出餐倒计时\n8分41秒\n您设置的承诺出餐时间将用于店铺出餐超时率的考核，请尽快确认出餐\n上报出餐\n12:15\n已呼叫配送\n展开进度\n3种商品，共3件\n收起\n备注:\n依据餐量提供餐具\n鸡蛋肉丝炒米粉-1人份[微辣]\nx1\n¥15.80\n卤鸡腿（1个）\nx1\n¥10.80\n“现蒸”柳叶蒸饺（8个）\nx1\n¥9.80\n打包费\n¥5.00\n商家对顾客的活动补贴\n-¥19.80\n商家优惠后金额\n¥21.60\n技术服务费\n-¥1.04\n商家配送费\n-¥3.80\n预计收入\n预计收入=商家优惠后金额-公益及其他支出-技术服务费-商家配送费\n¥15.46\n顾客实际支付\n下单时间: 05-27 12:14\n单号: 8042790074710995447复制\n取消订单打 印";
//        getDaySn(str);
//        getTranNo(str);
//        getxdsj(str);
//
//
//    }

    private static String getDaySn(String str) {
        Pattern pattern = Pattern.compile(Pattern.quote("#") + "(.*?)" + Pattern.quote("\n"));
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {

            return matcher.group(1);
        }
        return null;
    }

    private static String getTranNo(String str) {
        Pattern pattern = Pattern.compile(Pattern.quote("单号: ") + "(.*?)" + Pattern.quote("复制"));
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {

            return matcher.group(1);
        }
        return null;
    }

    private static String getxdsj(String str) {
        Pattern pattern = Pattern.compile(Pattern.quote("下单时间: ") + "(.*?)" + Pattern.quote("\n"));
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {

            return matcher.group(1);
        }
        return null;
    }

    public static void main(String[] args) {
        int currentYear = Year.now().getValue();

        System.out.println("当前年份是: " + currentYear);
    }

    public List<Result>  getOrder(String shopId){
        List<Result> orders = new ArrayList<>();
        WebDriver webDriver = webDriverMapService.getWebdrivers(shopId);
        WebElement element = webDriver.findElement(By.className("ant-spin-container"));
        List<WebElement> elements = element.findElements(By.xpath("./div"));
        for (WebElement webElement : elements) {
            try {
                Result result = new Result();
                String text = webElement.getText();
           //     text =  "#27\n13:07 前送达\n骑士已接单\n雨女士\n超级会员\n近180天第1次下单\n联系不上\n2.08km ·杭州金金酒类有限公司\n查看地图\n承诺出餐倒计时\n8分41秒\n您设置的承诺出餐时间将用于店铺出餐超时率的考核，请尽快确认出餐\n上报出餐\n12:06\n骑士已接单\n1.5km | 骑士:陈鹏名蜂鸟准时达\n展开进度\n1种商品，共1件\n收起\n备注:\n米饭上浇点汤汁 / 依据餐量提供餐具\n⭐【经典鸭腿+2时蔬+黄豆+卤蛋】套餐饭\nx1\n¥21.80\n打包费\n¥2.00\n商家对顾客的活动补贴\n-¥8.80\n商家优惠后金额\n¥15.00\n技术服务费\n-¥0.96\n商家配送费\n-¥3.20\n预计收入\n预计收入=商家优惠后金额-公益及其他支出-技术服务费-商家配送费\n¥10.34\n顾客实际支付\n下单时间: 05-27 12:05\n单号: 8018730074824829205复制\n取消订单打 印";
                String getxdsj = getxdsj(text);
                String tranNo = getTranNo(text);
                String daySn = getDaySn(text);
                int currentYear = Year.now().getValue();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                Date date = formatter.parse(currentYear+"-"+getxdsj);
                result.setActiveTime(date);
                Header header = new Header();
                header.setDaySn(daySn);
                result.setHeader(header);
                result.setId(tranNo);
                result.setShopId(Integer.parseInt(shopId));
                orders.add(result);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        return orders;
    }


    public void out(String shopId ,String outtranNo){
        WebDriver webDriver = webDriverMapService.getWebdrivers(shopId);
        WebElement element = webDriver.findElement(By.className("ant-spin-container"));
        List<WebElement> elements = element.findElements(By.xpath("./div"));
        for (WebElement webElement : elements) {
            try {
                String text = webElement.getText();
                if(text.contains(outtranNo)){
                    webElement.findElement(By.className("tooltip__1uoH_TOud-p-1ulUZrqrBE")).click();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
